﻿using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.DALTools
{
    public static class TokenUtils
    {
        public static Worker GetWorkerByToken(this string token, StarsEASContext context) {
            return context.Users.Where(use => use.Token.Equals(token)).Join(context.Workers, use => use.Username, wor => wor.WorkerId, (use, wor) =>new Worker() {
                WorkerId = wor.WorkerId,
                WorkerName = wor.WorkerName,
                WorkerAge = wor.WorkerAge,
                WorkerbackTime = wor.WorkerbackTime,
                WorkerCarid = wor.WorkerCarid,
                WorkerEmail = wor.WorkerEmail,
                WorkerLocal = wor.WorkerLocal,
                WorkerPhone = wor.WorkerPhone,
                WorkerSex = wor.WorkerSex,
            }).FirstOrDefault();
        }

        public static Model.Models.Student GetStudentByToken(this string token, StarsEASContext context){
           return (from stu in context.Students
                 join user in context.Users on stu.StuId equals user.Username
                 where user.Token.Equals(token)
                 select new Model.Models.Student()
                 {
                     StuId = stu.StuId,
                     StuName = stu.StuName,
                     StuAge = stu.StuAge,
                     StuSex = stu.StuSex,
                     StuCarid = stu.StuCarid,
                     ClassId = stu.ClassId,
                     StubackTime = stu.StubackTime,
                     StuPhone = stu.StuPhone,
                     PoliticalLandscape = stu.PoliticalLandscape,
                     NativePlace = stu.NativePlace,
                     National = stu.National,
                     StuEmail = stu.StuEmail
                }).FirstOrDefault();
        }

        public static bool IsStudent(this string token, StarsEASContext context) { 
            var user = context.Users.FirstOrDefault(u=>u.Token.Equals(token));
            if (user == null || user.PositionId == 1) return true;
            return false;
        }

        
    }
}
